# pip install jinja2
from jinja2 import Template
from datetime import datetime

# 1. 会变的数据 ----------------------------------------------------------
tab_name = "SBS3.ACTVHH"
user       = "SBS3"
tab_comm   = "传票历史文件"
cols_amt   = 30
recs_amt   = 10
sep        = "|"
part_info  = 1
sys_name   = "SBS"
sys_ver    = "V03.01"
sys_comm   = "UNIX综合业务系统"
infc_id    = "HB"
back_time  = datetime.now().strftime("%Y%m%d")   # 20250925
daterange  = "ERYDAT"

# 字段列表（按需增删）
columns = [
    {"colname": "ORGIDT", "type": "varchar", "length": 4, "colcomm": "机构号"},
    {"colname": "TLRNUM", "type": "varchar", "length": 4, "colcomm": "柜员号"},
    {"colname": "VCHSET", "type": "number",  "length": 4, "colcomm": "输入当天套顺序号"},
    {"colname": "SETSEQ", "type": "number",  "length": 2, "colcomm": "输入当天套内顺序号"},
    {"colname": "ERYDAT", "type": "date",    "length": 8, "colcomm": "记帐日"},

]

# 2. XML 模板 ------------------------------------------------------------
tmpl = Template('''\
<?xml version="1.0" ?>
<config>
    <tabname>{{ tab_name }}</tabname>
    <user>{{ user }}</user>
    <tabcomm>{{ tab_comm }}</tabcomm>
    <columnsamount>{{ cols_amt }}</columnsamount>
    <recordsamount>{{ recs_amt }}</recordsamount>
    <seperator>{{ sep }}</seperator>
    <partinfo>{{ part_info }}</partinfo>
    <system>
        <sysname>{{ sys_name }}</sysname>
        <sysversion>{{ sys_ver }}</sysversion>
        <syscomm>{{ sys_comm }}</syscomm>
        <infcenterid>{{ infc_id }}</infcenterid>
        <regcod></regcod>
    </system>
    <backup>
        <time>{{ back_time }}</time>
        <type>backbackup</type>
    </backup>
    <export>
        <daterangecols>{{ daterange }}</daterangecols>
    </export>
    <columns>
        {% for col in columns %}
        <column>
            <colname>{{ col.colname }}</colname>
            <type>{{ col.type }}</type>
            <length>{{ col.length }}</length>
            <colcomm>{{ col.colcomm }}</colcomm>
        </column>
        {% endfor %}
    </columns>
</config>''')

# 3. 渲染并保存 ----------------------------------------------------------
xml_str = tmpl.render(**locals())      # 直接把当前所有变量扔进去
out_file = f"{tab_name}.xml"
with open(out_file, "w", encoding="utf-8") as f:
    f.write(xml_str)

print(f"已生成 {out_file}")

